package com.liuceng.zen.pojo.domain;

import com.liuceng.zen.cache.GlobalCache;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author liyuhang
 */
@Data
public class Member extends BaseDomain {
  private String address;
  private BigDecimal amount;
  private BigDecimal balance;
  private String email;
  private Boolean enabled;
  private Boolean locked;
  private String lockKey;
  private Date lockedAt;
  private Date loginAt;
  private Short loginFailureCount;
  private String loginIp;
  private String phone;
  private String username;
  private String password;
  private String registerIp;
  private Date safeKeyExpireAt;
  private String safeKeyValue;
  private Integer province;
  private Integer city;
  private Integer area;
  private Short rank;
  private String companyName;
  private String corporateRegNo;
  private String siteName;
  private String qq;
  private Integer adminId;
  private String mobile;
  private String zipCode;
  private BigDecimal account;
  private String code;
  private Short industryType;
  private Short industrySubType;
  private Short operationType;
  private Integer adminDepartmentId;
  private String salt;
  private Date balanceCalculatedAt;

  private Integer coefficientType;
  private String avatarPath;
  private Integer avatarId;
  private String updatedAtToString;
  //
  private String provinceStr;
  private String cityStr;
  private String areaStr;
  private String payOfLinkman;

  private Integer limitAdCount;
  private Integer applyId;
  private BigDecimal pcMinAdOffer;
  private BigDecimal mobileMinAdOffer;

  //


  public static final String LOGINNAME_PATTERN =
      "^[\\u4E00-\\u9FA5\\uf900-\\ufa2d_a-zA-Z][\\u4E00-\\u9FA5\\uf900-\\ufa2d\\w]{3,19}$";
  public static final String EMAIL_PATTERN =
      "^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?";
  public static final String MOBILE_PHONE_NUMBER_PATTERN =
      "^0{0,1}(13[0-9]|15[0-9]|14[0-9]|18[0-9])[0-9]{8}$";
  public static final int LOGINNAME_MIN_LENGTH = 4;
  public static final int LOGINNAME_MAX_LENGTH = 20;
  public static final int PASSWORD_MIN_LENGTH = 6;
  public static final int PASSWORD_MAX_LENGTH = 50;

  public static final int VERIFYCODE_LENGTH = 4;
  //

  private String getMaskedStr(String field, String mask) {
    if (StringUtils.isBlank(field)) {
      return "";
    }
    int index = 0;
    // 判断哪个长一点，取短的
    int fieldLength = field.length();
    int maskLength = mask.length();
    int len = fieldLength > maskLength ? maskLength : fieldLength;
    //
    StringBuilder maskedStr = new StringBuilder();
    for (int i = 0; i < len; i++) {
      char c = mask.charAt(i);
      if (c == '#') {
        maskedStr.append(field.charAt(index));
      } else {
        maskedStr.append('*');
      }
      index++;
    }
    // 如果filed长度比mask长，则需要把剩下的补上
    if (fieldLength > maskLength) {
      maskedStr.append(field.substring(len));
    }
    //
    return maskedStr.toString();
  }

  public String getProvinceDisplay() {
    return getRegionDisplay(province);
  }

  public String getCityDisplay() {
    return getRegionDisplay(city);
  }

  public String getAreaDisplay() {
    return getRegionDisplay(area);
  }

  public String getPcaDisplay() {
    return getProvinceDisplay() + " " + getCityDisplay() + " " + getAreaDisplay();
  }

  private String getRegionDisplay(Integer id) {
    Map<String, Object> regionMap = GlobalCache.regionsMap.get(id);
    if (regionMap != null) {
      return (String) regionMap.get("name");
    } else {
      return "未知";
    }
  }

  public String getUpdatedAtToString() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    if (super.getUpdatedAt() == null) {
      return "";
    } else {
      return sdf.format(super.getUpdatedAt());
    }
  }
}
